home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / indents.zip / indent.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  60KB  |  1,441 lines

  1. /**
  2.  * Copyright (c) 1985 Sun Microsystems, Inc.
  3.  * Copyright (c) 1980 The Regents of the University of California.
  4.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted provided
  8.  * that the above copyright notice and this paragraph are duplicated in all
  9.  * such forms and that any documentation, advertising materials, and other
  10.  * materials related to such distribution and use acknowledge that the
  11.  * software was developed by the University of California, Berkeley, the
  12.  * University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  13.  * either University or Sun Microsystems may not be used to endorse or
  14.  * promote products derived from this software without specific prior written
  15.  * permission. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  17.  * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #include "globals.h"
  21. #include "codes.h"
  22.  
  23. #ifndef lint
  24. # ifndef ANSIC
  25. char            copyright[] =
  26. "@(#) Copyright 1989 Object Design, Inc.\n\
  27.  @(#) Copyright (c) 1985 Sun Microsystems, Inc.\n\
  28.  @(#) Copyright (c) 1980 The Regents of the University of California.\n\
  29.  @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.\n\
  30.  All rights reserved.\n";
  31.  
  32. static char     sccsid[] = "@(#)indent.c    6.0 (Berkeley) 92/06/15";
  33. # endif         /* ANSIC */
  34. #endif          /* not lint */
  35.  
  36. #include <string.h>
  37. #include <stdlib.h>
  38.  
  39. #ifdef BSD
  40. #include <sys/param.h>
  41. #include <unistd.h>
  42. #endif          /* BSD */
  43.  
  44. #ifndef MAXPATHLEN
  45. #define MAXPATHLEN 1024
  46. #endif          /* MAXPATHLEN */
  47.  
  48. #include <ctype.h>
  49.  
  50. #define free(ptr) if (ptr != NULL) {(void) free(ptr); ptr = NULL;}  /* think about it. PETER */
  51.  
  52. char           *in_name = "Standard Input"; /* will always point to name of
  53.                                              * input file */
  54. char           *out_name = "Standard Output";   /* will always point to name
  55.                                                  * of output file */
  56. char            bakfile[MAXPATHLEN] = "";
  57.  
  58. #ifdef ANSIC
  59. static void     mainloop(void);
  60. static void     err(char *msg);
  61. static void     bakcopy(void);
  62. static void     usage(void);
  63. #endif          /* ANSIC */
  64.  
  65. #ifdef ANSIC
  66. int             main(int argc, char *argv[])
  67. #else           /* ANSIC */
  68. main(argc, argv)
  69.     int             argc;
  70.     char          **argv;
  71. #endif          /* ANSIC */
  72. {
  73.     int             i;          /* local loop counter */
  74.     char            filename[MAXPATHLEN] = "";  /* input file name */
  75.     int             fnamelength;/* input file name length */
  76.     FILE           *stream;
  77.  
  78.     /*--------------------------------------------------
  79.                         COMMAND LINE SCAN
  80.       --------------------------------------------------*/
  81.  
  82.     if (argc < 2) {
  83.         usage();
  84.         show_options = 1;
  85.     }
  86.     set_defaults();
  87.     if (show_options) {
  88.         list_options("Compile time defaults are:");
  89.         fprintf(stderr, "\n");
  90.     }
  91.     for (i = 1; i < argc; ++i)
  92.         if (strcmp(argv[i], "-npro") == 0)
  93.             break;
  94.     if (i >= argc)
  95.         set_profile(argv[0]);
  96.  
  97.     for (i = 1; i < argc; ++i) {/* look through args (if any) for changes to
  98.                                  * defaults */
  99.         if (argv[i][0] == '-') {/* flag on parameter */
  100.             set_option(argv[i]);
  101.         }
  102.     }
  103.  
  104.     if ((useStdio) || (show_options)) { /* only do stdio, ignore any
  105.                                          * specified files */
  106.         mainloop();
  107.     } else {
  108.         for (i = 1; i < argc; ++i) {    /* look through args (if any) for
  109.                                          * filenames */
  110.             if (argv[i][0] != '-') {    /* no flag on parameter */
  111.                 if (argv[i][0] == '@' && strlen(argv[i]) > 2) {
  112.  
  113.                     /* it's a list of file names, get the filename */
  114.  
  115.                     fnamelength = 0;
  116.                     filename[0] = 0;
  117.                     while (argv[i][fnamelength + 1] != 0) {
  118.                         filename[fnamelength] = argv[i][fnamelength + 1];
  119.                         fnamelength++;
  120.                     }
  121.                     filename[fnamelength] = 0;
  122.  
  123.                     /* open the file */
  124.  
  125.                     fprintf(stderr, "Opening list file: %s.\n", filename);
  126.                     if ((stream = fopen(filename, "r")) == 0) {
  127.                         err(filename);
  128.                     }
  129.                     /* read the input file for file names */
  130.  
  131.                     while (!feof(stream)) {
  132.                         filename[0] = 0;
  133.                         fgets(filename, MAXPATHLEN - 1, stream);
  134.                         if ((fnamelength = strlen(filename)) > 1) {
  135.                             if (filename[fnamelength - 1] == '\n')
  136.                                 filename[fnamelength - 1] = 0;  /* remove newline char */
  137.                             in_name = filename; /* remember name of input
  138.                                                  * file */
  139.                             mainloop();
  140.                         }
  141.                     }
  142.                 } else {        /* assume it's a source file */
  143.                     in_name = argv[i];  /* remember name of input file */
  144.                     mainloop();
  145.                 }
  146.             }
  147.         }                       /* end of for */
  148.     }
  149.     exit(0);                    /* closes the list file (and anything else) */
  150.     return 0;                   /* necessary to stop ANSI compilers moaning */
  151. }
  152.  
  153.  
  154.  
  155. #ifdef ANSIC
  156. static void     mainloop(void)
  157. #else           /* ANSIC */
  158. mainloop()
  159. #endif          /* ANSIC */
  160. {
  161.     char           *codebuf = NULL; /* buffer for code section */
  162.     char           *labbuf = NULL;  /* buffer for label */
  163.     char           *combuf = NULL;  /* buffer for comments */
  164.  
  165.     extern int      found_err;  /* flag set in diag() on error */
  166.     int             dec_ind;    /* current indentation for declarations */
  167.     int             di_stack[20];   /* a stack of structure indentation
  168.                                      * levels */
  169.     int             flushed_nl; /* used when buffering up comments to
  170.                                  * remember that a newline was passed over */
  171.     int             force_nl;   /* when true, code must be broken */
  172.     int             hd_type = 0;/* used to store type of stmt for if (...),
  173.                                  * for (...), etc */
  174.     register int    i;          /* local loop counter */
  175.     int             scase;      /* set to true when we see a case, so we will
  176.                                  * know what to do with the following colon */
  177.     int             sp_sw;      /* when true, we are in the expression of
  178.                                  * if(...), while(...), etc. */
  179.     int             squest;     /* when this is positive, we have seen a ?
  180.                                  * without the matching : in a <c>?<s>:<s>
  181.                                  * construct */
  182.     register char  *t_ptr = NULL;   /* used for copying tokens */
  183.     int             type_code;  /* the type of token, returned by lexi */
  184.  
  185.     int             last_else = 0;  /* true iff last keyword was an else */
  186.  
  187.     int             just_saw_cc_cmnt = 0;   /* true if '/''/' comment was
  188.                                              * just emitted (jrs 8jun92) */
  189.     int             stringptr;
  190.  
  191.     char            save_com[sc_size] = ""; /* input text is saved here when
  192.                                              * looking for the brace after an
  193.                                              * if, while, etc. */
  194.     char           *sc_end = NULL;  /* pointer into save_com buffer */
  195.  
  196.     /*-----------------------------------------------*\
  197.     |                INITIALISATION                   |
  198.     \*-----------------------------------------------*/
  199.  
  200.     else_or_endif = false;      /* -cp PETER */
  201.  
  202.     ps.p_stack[0] =